package com.ludaxu.location;

import java.util.List;

import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {

	private TextView tvLocation;
	private LocationManager lm;
	private MyLocationListener listener;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tvLocation = (TextView) findViewById(R.id.tv_location);

		lm = (LocationManager) getSystemService(LOCATION_SERVICE);
		List<String> allProviders = lm.getAllProviders();// 获取所有的位置提供者,也就是定位方式
		System.out.println(allProviders);
		listener = new MyLocationListener();
		// 定位方式,最短的更新时间(s),最短的更新距离(m),监听
		lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);

	}

	class MyLocationListener implements LocationListener {

		// 位置发生改变
		@Override
		public void onLocationChanged(Location location) {
			double longitude = location.getLongitude();// 经度
			double latitude = location.getLatitude();// 纬度
			double altitude = location.getAltitude();// 海拔.不稳定,有时拿的到,有时拿不到
			float accuracy = location.getAccuracy();// 精确度
			tvLocation.setText("经度:" + longitude + "纬度:" + latitude + "海拔:"
					+ altitude + "精确度:" + accuracy);

		}

		// 用户关闭GPS
		@Override
		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub

		}

		// 用户打开GPS
		@Override
		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub

		}

		// 位置提供者的状态发生变化,比如是信号的原因无法接收信号
		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub

		}

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		lm.removeUpdates(listener);//当activity销毁时,关闭监听服务
	}

}
